<?php
namespace app\controller\api;

use app\BaseController;
use app\model\GoodsModel;

class Goods extends BaseController
{
    // 获取商品列表
    public function getGoodsList()
    {
        // 接收搜索参数
        $title = $this->request->param('title', '');
        $where = [];
        if (!empty($title)) {
            $where[] = ['title', 'like', '%'.$title.'%'];
        }
        // 接收排序参数
        $orderField = $this->request->param('order_field/s', 'sale');
        $orderType = $this->request->param('order_type/s', 'desc');
        // 分页
        $page = $this->request->param('page/d', 1);
        $pageSize = $this->request->param('page_size/d', 6);

        $list = GoodsModel::field('id,image,title,is_ziying,price,now_price,sale')
            ->order($orderField, $orderType)
            ->page($page, $pageSize)
            ->where($where)
            ->select();

        $this->success('商品列表', $list);
        /*
        $arr = [
            [
                'name' => 'color',
                'text' => '颜色',
                'list' => ['红色', '黄色', '白色']
            ],
            [
                'name' => 'size',
                'text' => '尺寸',
                'list' => ['36', '37', '38', '39', '40']
            ]
        ];
        */
    }

    // 获取商品详情
    public function getGoodsInfo()
    {
        $id = $this->request->param('id/d');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $goods = GoodsModel::withoutField('is_saleing,create_time,update_time,delete_time')->find($id);
        $goods['images'] = explode(',', $goods['images']);
        $goods['sku'] = json_decode($goods['sku'], true);
        $this->success('商品详情', $goods);
    }
}
